<template>
  <el-table ref="multipleTableRef" :data="list" style="width: 100%" @selection-change="handleSelectionChange" row-key="date">
    <el-table-column type="selection" width="55" :reserve-selection="true" />
    <el-table-column label="Date" width="120">
      <template #default="scope">{{ scope.row.date }}</template>
    </el-table-column>
    <el-table-column property="name" label="Name" width="120" />
    <el-table-column property="address" label="Address" show-overflow-tooltip />
  </el-table>
  <el-pagination
    v-model:current-page="currentPage"
    v-model:page-size="pageSize"
    :page-sizes="[2, 3, 4, 5]"
    layout="total, sizes, prev, pager, next, jumper"
    :total="8"
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
  />
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { ElTable } from 'element-plus'

interface User {
  date: string
  name: string
  address: string
}

const multipleTableRef = ref<InstanceType<typeof ElTable>>()
const multipleSelection = ref<User[]>([])
const handleSelectionChange = (val: User[]) => {
  multipleSelection.value = val
}

const tableData: User[] = [
  {
    date: '2016-05-03',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles'
  },
  {
    date: '2016-05-02',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles'
  },
  {
    date: '2016-05-04',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles'
  },
  {
    date: '2016-05-01',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles'
  },
  {
    date: '2016-05-08',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles'
  },
  {
    date: '2016-05-06',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles'
  },
  {
    date: '2016-05-07',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles'
  }
]

const currentPage = ref(1)
const pageSize = ref(3)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  filterList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  filterList()
}
const list = ref<User[]>([])
const filterList = () => {
  list.value = []
  tableData.forEach((item: User, key: number) => {
    if (key >= (currentPage.value - 1) * pageSize.value && key < (currentPage.value) * pageSize.value) {
      list.value.push(item)
    }
  })
}
onMounted(() => {
  filterList()
})
</script>
